package com.wlc.collection;

import java.util.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description HashMap 键值对存储数据
 * @date 2021-07-12 10:51
 */
public class HashMapDemo {
    /**
     * 生成 200000个 对象，放入到Map中   对象 的属性名 name = hero-XXXX
     * 然后查找 hero-5580
     * <p>
     * HashMap 键不可以重复
     */
    public void method01() {

        List<Hero> heroList = new ArrayList();
        for (int i = 0; i < 200000; i++) {
            heroList.add(new Hero("hero-" + random(), random() + 1));
        }

        // 向map中放入数据
        Map<String, List<Hero>> heroMap = new HashMap();
        for (Hero h : heroList) {
            List<Hero> heroList1 = heroMap.get(h.getName());
            // 因为 map 的key 不可以重复，所以要查找 名字是否有重复，也就是heroList1 为空；
            // 如果为空，就说明没有重复
            // 等下一次循环，再次判断是否有重复，如果有重复，直接把 heroList1 添加对象h 就可以了
            if (null == heroList1) {
                heroList1 = new ArrayList<>();
                heroMap.put(h.getName(), heroList1);
            }
            heroList1.add(h);
        }
        System.out.println("heroMap.size() = " + heroMap.size());

        List<Hero> heroes = heroMap.get("hero-2560");
        System.out.println("开始查找 英雄名称为 hero-2560");
        System.out.println("map形式查找：");
        findByMap(heroMap);
        System.out.println("for循环遍历查找：");
        findByFor(heroList);
    }

    public void findByFor(List<Hero> list) {
        long start = System.currentTimeMillis();
        ArrayList<Hero> heroes = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if ("hero-2560".equals(list.get(i).getName())) {
                heroes.add(list.get(i));
            }
        }
        long end = System.currentTimeMillis();
        heroes.forEach(System.out::println);
        System.out.printf("共查找到%d个对象，共耗时%d 毫秒。", heroes.size(), end - start);
        System.out.println("");
    }

    /**
     * 通过map查找
     *
     * @param map
     */
    public void findByMap(Map<String, List<Hero>> map) {
        long start = System.currentTimeMillis();
        List<Hero> heroes = map.get("hero-2560");
        long end = System.currentTimeMillis();
        heroes.forEach(System.out::println);
        System.out.printf("共查找到%d个对象，共耗时%d 毫秒。", heroes.size(), end - start);
    }

    public static int random() {
        int random = (int) (Math.random() * 9000) + 1000;
        return random;
    }

    public void method02() {
        HashMap<String, List<Object>> map = new HashMap<>();

        for (int i = 0; i < 3; i++) {
            List<Object> objects = map.get("a");
            if (null == objects) {
                objects = new ArrayList<>();
                map.put("a", objects);
            }
            objects.add(i);
        }
        List<Object> objects = map.get("a");
        for (int i = 0; i < objects.size(); i++) {
            System.out.println(objects.get(i));
        }

    }

    /**
     * HashMap 和HashTable
     */
    public void method03() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("abc", null);
        Hashtable<String, Object> hashtable = new Hashtable<>();
        hashtable.put("abc", null);
        System.out.println(hashMap.get("abc"));
        System.out.println(hashtable.get("abc"));
    }

    public static void main(String[] args) {
        new HashMapDemo().method03();

    }
}
